package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.core.api.MdMedTbFldService;
import com.dw.etl.core.bean.model.MdMedTbFld;
import com.dw.etl.core.bean.vo.LayMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * mdMedTbFld控制器
 * Created by hozeData on 2021/07/08.
 */
@RestController
@RequestMapping("/etl/mdMedTbFld/")
@Api(tags="表字段")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdMedTbFldController {

    @Autowired
    MdMedTbFldService mdMedTbFldService;

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg find(String idTb) {
        MdMedTbFld mdMedTbFld = new MdMedTbFld();
        mdMedTbFld.setIdTb(idTb);
        List<MdMedTbFld> pageVO = mdMedTbFldService.findByModel(mdMedTbFld);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="findDimFld", method=RequestMethod.POST)
    public LayMsg findDimFld(String idTb) {
        MdMedTbFld mdMedTbFld = new MdMedTbFld();
        mdMedTbFld.setIdTb(idTb);
        List<MdMedTbFld> pageVO = mdMedTbFldService.findByModel(mdMedTbFld);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="保存")
    @RequestMapping(value="saveList", method=RequestMethod.POST)
    public BaseResult saveList(@RequestBody List<MdMedTbFld> fieldList, HttpServletRequest request)  {
        mdMedTbFldService.save(fieldList);
        MdMedTbFld field = new MdMedTbFld();
        field.setIdTb(fieldList.get(0).getIdTb());
        List<MdMedTbFld> pageVO = mdMedTbFldService.findByModel(field);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idTbFld", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idTbFld, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idTbFld)){
            throw new BaseException("请选择要编辑的项");
        }
        MdMedTbFld mdMedTbFld = mdMedTbFldService.getById(idTbFld);
        return new BaseResult(CommonResultConstant.SUCCESS, mdMedTbFld);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="mdMedTbFld", value="实体", paramType="body", required=true, dataType="MdMedTbFld")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody MdMedTbFld mdMedTbFld, HttpServletRequest request)  {
        int num = mdMedTbFldService.updateByIdSelective(mdMedTbFld);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idTbFld", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idTbFld, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idTbFld)){
            throw new BaseException("请选择要删除的项");
        }
        int num = mdMedTbFldService.deleteById(idTbFld);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="查询有效表字段")
    @RequestMapping(value="findValidTbField", method=RequestMethod.GET)
    public LayMsg findValidTbField(String idTb) {
        List<MdMedTbFld> pageVO = mdMedTbFldService.findValidTbField(idTb);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="查询维度字段")
    @RequestMapping(value="findDimFieldByIdIndex", method=RequestMethod.GET)
    public LayMsg findTbDimField(String idTb) {
        List<MdMedTbFld> pageVO = mdMedTbFldService.findValidTbField(idTb);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

}
